package com.jmpt.fangbohui.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.bean.OldHouseBean;
import com.jmpt.fangbohui.utils.ImageLoaderHelper;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public class OldHouseListAdapter extends BaseAdapter {

	private Context mContext;
	public List<OldHouseBean> houseList = new ArrayList<OldHouseBean>();

	public OldHouseListAdapter(Context context) {
		this.mContext = context;
	}

	@Override
	public int getCount() {
		return houseList.size();
	}

	@Override
	public Object getItem(int position) {
		return houseList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return houseList.get(position).HouseID;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO 填充旧房条目
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.item_list_oldhouse,
					null);
		}
		Holder holder = getHolder(convertView);
		OldHouseBean bean = houseList.get(position);
		ImageLoaderHelper.displayImage(bean.H_SmallPic, holder.housePhoto);
		holder.houseTitle.setText(bean.H_Title);
		holder.houseRoomAndHall.setText(bean.H_Room + "房" + bean.H_Hall + "厅");
		holder.houseProportion.setText(bean.H_MJ + "平米");
		holder.houseTotalPrice.setText(String.valueOf(bean.H_SalePrice + "万"));
		holder.houseAddress.setText(bean.H_DZ);
		if (!TextUtils.isEmpty(bean.ZXName)) {
			holder.houseFitment.setVisibility(View.VISIBLE);
			holder.houseFitment.setText(bean.ZXName);
		} else {
			holder.houseFitment.setVisibility(View.INVISIBLE);
		}

        holder.houseContacts.setText(TextUtils.isEmpty(bean.H_SendInfoName) ? "" : "联系人：" + bean.H_SendInfoName);
        holder.houseContactsTel.setText(TextUtils.isEmpty(bean.H_SendInfoTel) ? "" : "(" + bean.H_SendInfoTel + ")");
        
		return convertView;

	}

	private Holder getHolder(final View view) {
		Holder holder = (Holder) view.getTag();
		if (holder == null) {
			holder = new Holder(view);
			view.setTag(holder);
		}
		return holder;
	}

	private class Holder {
		public ImageView housePhoto;
		public TextView houseTitle;
		public TextView houseRoomAndHall;
		public TextView houseProportion;
		public TextView houseTotalPrice;
		public TextView houseAddress;
		public TextView houseFitment; // 装修
		public TextView houseContacts;
        public TextView houseContactsTel;
		public Holder(View view) {
			housePhoto = (ImageView) view
					.findViewById(R.id.iv_oldhouse_item_photo);
			houseTitle = (TextView) view
					.findViewById(R.id.tv_oldhouse_item_title);
			houseRoomAndHall = (TextView) view
					.findViewById(R.id.tv_oldhouse_item_roomAndHall);
			houseProportion = (TextView) view
					.findViewById(R.id.tv_oldhouse_item_proportion);
			houseTotalPrice = (TextView) view
					.findViewById(R.id.tv_oldhouse_item_totalPrice);
			houseAddress = (TextView) view
					.findViewById(R.id.tv_oldhouse_item_addr);
			houseFitment = (TextView) view
					.findViewById(R.id.tv_oldhouse_item_fitment);
			houseContacts = (TextView) view.findViewById(R.id.tv_oldhouse_item_contacts);
        	houseContactsTel = (TextView) view.findViewById(R.id.tv_oldhouse_item_tel);
		}
	}
}
